﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace CommunitySystem.PageView.公务员选拔
{
    public partial class ExamPublishControl : UserControl
    {
        private CommunitySystem.PublicOfficialSelectionService.PublicOfficialSelectionServiceClient client = null;
        private ExamePublishWindow window = null;
        public ExamPublishControl()
        {
            InitializeComponent();
            client = new PublicOfficialSelectionService.PublicOfficialSelectionServiceClient();
            Register();
            Refresh();
            SearchText();
        }

        private void SearchText()
        {
            ObservableCollection<ComBoClass> typeCollection = new ObservableCollection<ComBoClass>();
            typeCollection.Add(new ComBoClass() { Text = "全部", value = -1 });
            typeCollection.Add(new ComBoClass() { Text = "竞争性选拔", value = 0 });
            typeCollection.Add(new ComBoClass() { Text = "选调交流", value = 1 });
            typeCB.ItemsSource = typeCollection;
            typeCB.SelectedIndex = 0;

            ObservableCollection<ComBoClass> statusCollection = new ObservableCollection<ComBoClass>();
            statusCollection.Add(new ComBoClass() { Text = "全部", value = -1 });
            statusCollection.Add(new ComBoClass() { Text = "暂存", value = 0 });
            statusCollection.Add(new ComBoClass() { Text = "发布", value = 1 });
            StatusCB.ItemsSource = statusCollection;
            StatusCB.SelectedIndex = 0;
            
        }

        private void NewData_Click(object sender, RoutedEventArgs e)
        {
            window = new ExamePublishWindow(0);
            window.Closed += new EventHandler(window_Closed);
            window.Show();
        }

        void window_Closed(object sender, EventArgs e)
        {
            ChildWindow cw = sender as ChildWindow;
            if (cw != null && cw.DialogResult == true)
            {
                Refresh();
            }
        }

        private void EditData_Click(object sender, RoutedEventArgs e)
        {
            if (DGRole.SelectedItem != null)
            {
                var entity = DGRole.SelectedItem as PublicOfficialSelectionService.PublicOfficialInfo;
                if (entity != null)
                {
                    window = new ExamePublishWindow(entity.ID);
                    window.Closed += new EventHandler(window_Closed);
                    window.Show();
                }
            }
        }

        private void DeleteData_Click(object sender, RoutedEventArgs e)
        {
            SilverlightMessageBoxLibrary.CustomMessage cm = new SilverlightMessageBoxLibrary.CustomMessage("确认删除选中项?", SilverlightMessageBoxLibrary.CustomMessage.MessageType.Confirm, null);
            cm.OKButton.Click -= new RoutedEventHandler(OKButton_Click);
            cm.OKButton.Click += new RoutedEventHandler(OKButton_Click);
            cm.Show();
        }

        void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (DGRole.SelectedItem != null)
            {
                var entity = DGRole.SelectedItem as PublicOfficialSelectionService.PublicOfficialInfo;
                if (entity != null)
                {
                    client.DeletePublicOfficialInfoAsync(entity.ID);
                }
            }
        }

        private void dgXQ_LoadingRow(object sender, DataGridRowEventArgs e)
        {

        }

        
        private void Register()
        {
            client.GetPublicOfficialInfoCompleted += new EventHandler<PublicOfficialSelectionService.GetPublicOfficialInfoCompletedEventArgs>(client_GetPublicOfficialInfoCompleted);
            client.DeletePublicOfficialInfoCompleted += new EventHandler<PublicOfficialSelectionService.DeletePublicOfficialInfoCompletedEventArgs>(client_DeletePublicOfficialInfoCompleted);
        }

        void client_DeletePublicOfficialInfoCompleted(object sender, PublicOfficialSelectionService.DeletePublicOfficialInfoCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("删除成功!");
                Refresh();
            }
        }

        private void Refresh()
        {
            client.GetPublicOfficialInfoAsync("1=1");
        }

        void client_GetPublicOfficialInfoCompleted(object sender, PublicOfficialSelectionService.GetPublicOfficialInfoCompletedEventArgs e)
        {
            DGRole.ItemsSource = null;
            DGRole.ItemsSource = e.Result;
        }

        #region 左右拖动的条
        private GridLength _lastColLeftLength;
        private GridLength _lastColRightLength;
        private void GridSpliteTo_ToLeft(object sender, EventArgs e)
        {
            //已经到最左边，直接返回
            if (colLeft.Width.Value == 0.0)
            {
                return;
            }

            //
            if (colRight.Width.Value == 0)
            {
                colLeft.Width = _lastColLeftLength;
                colRight.Width = _lastColRightLength;
            }
            else
            {
                _lastColLeftLength = colLeft.Width;
                _lastColRightLength = colRight.Width;

                colLeft.Width = new GridLength(0);
                colRight.Width = new GridLength(150, GridUnitType.Star);
            }
        }

        private void GridSpliteTo_ToRight(object sender, EventArgs e)
        {
            if (colRight.Width.Value == 0)
            {
                return;
            }

            if (colLeft.Width.Value == 0)
            {
                colLeft.Width = _lastColLeftLength;
                colRight.Width = _lastColRightLength;
            }
            else
            {
                _lastColLeftLength = colLeft.Width;
                _lastColRightLength = colRight.Width;

                colRight.Width = new GridLength(0);
                colLeft.Width = new GridLength(300, GridUnitType.Star);
            }
        }
        #endregion

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string condition = string.Empty;
            if (!string.IsNullOrEmpty(tbName.Text))
            {
                condition = " name like '%" + tbName.Text + "%'";
            }
            var typeSelect = typeCB.SelectedItem as ComBoClass;
            var statusSelect = StatusCB.SelectedItem as ComBoClass;
            if (typeSelect != null && typeSelect.value != -1)
            {
                condition += string.IsNullOrEmpty(condition) ? "Type=" + typeSelect.value : " and Type=" + typeSelect.value;
            }
            if (statusSelect != null && statusSelect.value != -1)
            {
                condition += string.IsNullOrEmpty(condition) ? "Status=" + statusSelect.value : " and Status=" + statusSelect.value;
            }

            if (!string.IsNullOrEmpty(tbInputName.Text))
            {
                condition += string.IsNullOrEmpty(condition) ? " InputName like '%" + tbInputName.Text + "%'" : " and InputName like '%" + tbInputName.Text + "%'";
            }

            DateTime dtBegin = dpBegin.SelectedDate == null ? new DateTime(1900, 1, 1) : (DateTime)dpBegin.SelectedDate;
            DateTime dtEnd = dpEndi.SelectedDate == null ? DateTime.Now : (DateTime)dpEndi.SelectedDate;
            string time = "InputTime >='" + dtBegin.ToString("yyyy-MM-dd") + "' and InputTime<='" + dtEnd.ToString("yyyy-MM-dd") + " 23:59:59:999" + "'";
            condition += string.IsNullOrEmpty(condition) ? time : " and " + time;
            client.GetPublicOfficialInfoAsync(condition);
        }
    }

    public class ComBoClass
    {
        public string Text { get; set; }

        public int value { get; set; }
    }
}
